package site.brood.auth.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.brood.common.core.jwt.JwtUtil;
import site.brood.common.core.str.StrUtil;
import site.brood.common.core.constant.Constants;
import site.brood.common.core.user.LoginUserVO;
import site.brood.common.core.web.AjaxJson;
import site.brood.common.redis.service.RedisService;
import site.brood.common.security.service.TokenService;
import site.brood.common.security.util.SecurityUtil;
import site.brood.common.user.feign.UserFeign;
import site.brood.common.user.pojo.UserDto;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class LoginService {

    @Autowired
    private ValidateService validateService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserFeign userFeign;

    /**
     * 登录
     */
    public Map<String,Object> login(String username, String password){
        //检查参数
        validateService.checkLoginParams(username,password);

        // 查询用户信息
        AjaxJson<LoginUserVO> userResult = userFeign.getByName(username, Constants.FROM_SOURCE_INNER);

        //检查feign返回
        AjaxJson.checkResponse(userResult,"user");

        //检查返回用户数据
        validateService.checkLoginResult(username,password,userResult);
        //生成jwt格式access token
        //将用户详情写入redis
        Map<String, Object> tokenMap = tokenService.createToken(userResult.getData());
        validateService.recordLoginLog(username, true, "登录成功");
        return tokenMap;
    }

    /**
     * 注册
     */
    public void register(String username, String password)
    {
        //检查输入参数
        validateService.checkRegisterParams(username,password);

        // 注册用户信息
        UserDto userDto = new UserDto();
        userDto.setUsername(username);
        userDto.setNickname(username);
        userDto.setPassword(SecurityUtil.encryptPassword(password));
        AjaxJson registerResult = userFeign.add(userDto, Constants.FROM_SOURCE_INNER);
        validateService.checkRegisterResult(registerResult);
        validateService.recordLoginLog(username, true, "注册成功");
    }

    public void logout(HttpServletRequest request)
    {
        String token = SecurityUtil.getToken(request);
        if (StrUtil.isEmpty(token))
            return;

        String username = JwtUtil.getUsername(token);
        // 删除用户缓存记录
        SecurityUtil.logoutByToken(token);
        // 记录用户退出日志
        validateService.recordLoginLog(username, true, "退出成功");
    }



    /**
     * 删除用户缓存信息
     */
    public void delLoginUser(String token)
    {
        if (StrUtil.isNotEmpty(token))
        {
            String userkey = JwtUtil.getUserCacheKey(token);
            redisService.deleteObject(getTokenKey(userkey));
        }
    }

    private String getTokenKey(String token)
    {
        return Constants.REDIS_LOGIN_TOKEN_KEY_PREFIX + token;
    }



}
